package com.doctor.windflower_doctor.recordfragment;

import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.adapter.StatisticalListAdapter;
import com.doctor.windflower_doctor.entity.RecordBeen;
import com.doctor.windflower_doctor.fragment.BaseFragment;
import com.doctor.windflower_doctor.http.parser.impl.JsonHelper;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;

public class SleepFragment extends BaseFragment {
	private static SleepFragment baseFragment;
	private ListView listView;
	private StatisticalListAdapter statisticalListAdapter;
	private int type = 1;
	private List<RecordBeen> recordList;
	private TextView unit_text;
	private final String weightURL="http://192.168.199.162:3011/api/notes/v2/st?userid=543a374d623e8b2d2652e546"; 
	


	public static SleepFragment getInstance(Bundle bundle) {
		baseFragment = new SleepFragment();
		baseFragment.setArguments(bundle);
		return baseFragment;
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_weight_record, container, false);
	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		
		
	}

	@Override
	protected void findViews(View view) {
		listView = (ListView) view.findViewById(R.id.listView);
		listView.setDividerHeight(0);
		statisticalListAdapter = new StatisticalListAdapter(getActivity(),type);
		unit_text = (TextView) view.findViewById(R.id.unit_text);
		unit_text.setText("睡眠");
		new WeightAsynck().execute(weightURL);

	}

	@Override
	public void onResume() {
		/*NoteService noteService = new NoteService(getActivity());
		if (statisticalListAdapter.getCount() > 0)
			statisticalListAdapter.clearData();
		notesList = noteService.findByWeight();
		UserComparator userComparator = new UserComparator();
		Collections.sort(notesList, userComparator);
		for (int i = 0; i < notesList.size(); i++) {
			if (notesList.get(i).getWeight().equals("null")) {
				notesList.get(i).setWeight("0");
			}
		}
		statisticalListAdapter.addListData(notesList);*/
		super.onResume();
	}

	@Override
	public void onDestroyView() {
		baseFragment = null;
		super.onDestroyView();

	}

	@Override
	public void onStop() {
		baseFragment = null;
		super.onStop();

	}
	class WeightAsynck extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.getAddress(params[0]);
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result!=null){
				recordList=JsonHelper.record_Json(result);
				
				statisticalListAdapter.addListData(recordList);
				listView.setAdapter(statisticalListAdapter);
			}else{
				Toast.makeText(getActivity(), "请检查网络连接", Toast.LENGTH_SHORT).show();
			}
		
		}
	}
}
